﻿namespace Rail.Commons
{
    /// <summary>
    /// 泛型单例模式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SingletonProvider<T> where T : class, new()
    {
        private static T _instance = null;
        private static object _instanceLock = new object();

        private SingletonProvider()
        {
        }

        /// <summary>
        /// 用法：SingletonProvider&lt;T&gt;.Instance 获取该类的单例
        /// </summary>
        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_instanceLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new T();
                        }
                    }
                }
                return _instance;
            }
        }
    }
}